const http = uni.$u.http;

const get = (url, params, config) => {
  if (params._id !== undefined) {
    url += `/${params._id}`;
    delete params._id;
  }
  return http.get(url, {
    ...config,
    params,
  });
};

const httpDelete = (url, data, config) => {
  if (data._id) {
    url += `/${data._id}`;
    delete data._id;
  }
  return http.delete(url, data, config);
};

export function editAvatar(params = {}, config = {}) {
  return http.post("/baseplatform/wxUser/edit", params, {
    ...config,
    custom: { auth: true, toast: true, catch: false, ...(config.custom || {}) },
  });
}

export function getShopCategoryList(params = {}, config = {}) {
  return get("/shop/mobile/goods/category/search", params, {
    ...config,
    custom: {
      auth: false,
      toast: true,
      catch: false,
      ...(config.custom || {}),
    },
  });
}

export function getShopGoodsList(params = {}, config = {}) {
  return get("/shop/mobile/goods/list", params, {
    ...config,
    custom: {
      auth: false,
      toast: true,
      catch: false,
      ...(config.custom || {}),
    },
  });
}

export function getShopGoods(params = {}, config = {}) {
  return get("/shop/mobile/goods", params, {
    ...config,
    custom: {
      auth: false,
      toast: true,
      catch: false,
      ...(config.custom || {}),
    },
  });
}

export function getCartCount(params = {}, config = {}) {
  return get("/shop/mobile/cart/countByUser", params, {
    ...config,
    custom: { auth: true, toast: true, catch: false, ...(config.custom || {}) },
  });
}

export function postShopCart(params = {}, config = {}) {
  return http.post("/shop/mobile/cart", params, {
    ...config,
    custom: { auth: true, toast: true, catch: false, ...(config.custom || {}) },
  });
}

export function putShopCart(params = {}, config = {}) {
  return http.put("/shop/mobile/cart", params, {
    ...config,
    custom: { auth: true, toast: true, catch: false, ...(config.custom || {}) },
  });
}

export function deleteShopCart(params = {}, config = {}) {
  return httpDelete("/shop/mobile/cart", params, {
    ...config,
    custom: { auth: true, toast: true, catch: false, ...(config.custom || {}) },
  });
}

export function getShopCartList(params = {}, config = {}) {
  return get("/shop/mobile/cart/list", params, {
    ...config,
    custom: { auth: true, toast: true, catch: false, ...(config.custom || {}) },
  });
}

export function getAddressList(params = {}, config = {}) {
  return get("/shop/mobile/receiveAddress/listBySelf", params, {
    ...config,
    custom: { auth: true, toast: true, catch: false, ...(config.custom || {}) },
  });
}

export function getAddressArea(params = {}, config = {}) {
  return get("/baseplatform/area/getInitData", params, {
    ...config,
    custom: { auth: true, toast: true, catch: false, ...(config.custom || {}) },
  });
}

export function getAddressDetail(params = {}, config = {}) {
  return get(`/shop/mobile/receiveAddress`, params, {
    ...config,
    custom: { auth: true, toast: true, catch: false, ...(config.custom || {}) },
  });
}

export function getAddressNextAreaByCode(params = {}, config = {}) {
  return get(`/baseplatform/area/getNextAreaByCode`, params, {
    ...config,
    custom: { auth: true, toast: true, catch: false, ...(config.custom || {}) },
  });
}

export function deleteAddress(params = {}, config = {}) {
  return httpDelete(`/shop/mobile/receiveAddress`, params, {
    ...config,
    custom: { auth: true, toast: true, catch: false, ...(config.custom || {}) },
  });
}

export function editAddress(params = {}, config = {}) {
  return http.put(`/shop/mobile/receiveAddress`, params, {
    ...config,
    custom: { auth: true, toast: true, catch: false, ...(config.custom || {}) },
  });
}

export function addAddress(params = {}, config = {}) {
  return http.post(`/shop/mobile/receiveAddress`, params, {
    ...config,
    custom: { auth: true, toast: true, catch: false, ...(config.custom || {}) },
  });
}

export function getOneByCode(params = {}, config = {}) {
  return get(`/shop/mobile/postage/getOneByCode`, params, {
    ...config,
    custom: { auth: true, toast: true, catch: false, ...(config.custom || {}) },
  });
}

export function getPickUpList(params = {}, config = {}) {
  return get(`/shop/mobile/store/centers`, params, {
    ...config,
    custom: {
      auth: false,
      toast: true,
      catch: false,
      ...(config.custom || {}),
    },
  });
}

export function getActiveDetail(params = {}, config = {}) {
  return http.post(`/ticket/appletTicket/detail`, params, {
    ...config,
    custom: {
      auth: false,
      toast: true,
      catch: false,
      ...(config.custom || {}),
    },
  });
}

export function addTicOrder(params = {}, config = {}) {
  return http.post(`/order/order/addTicOrder`, params, {
    ...config,
    custom: { auth: true, toast: true, catch: false, ...(config.custom || {}) },
  });
}

// 获取订单列表
export function getlistOrderNew(params = {}, config = {}) {
  return get(`/ticket/appletTicket/listOrderNew`, params, {
    ...config,
    custom: { auth: true, toast: true, catch: false, ...(config.custom || {}) },
  });
}

// 获取订单列表
export function getlistOrderVenues(params = {}, config = {}) {
  return get(`/ticket/appletTicket/listOrderVenues`, params, {
    ...config,
    custom: { auth: true, toast: true, catch: false, ...(config.custom || {}) },
  });
}

// 获取订单详情体育场
export function getDetailOrder(params = {}, config = {}) {
  return get(`/ticket/appletTicket/detailVenuesOrder`, params, {
    ...config,
    custom: { auth: true, toast: true, catch: false, ...(config.custom || {}) },
  });
}

// 下订单
export function addOtherOrder(params = {}, config = {}) {
  return http.post(`/order/order/addOtherOrder`, params, {
    ...config,
    custom: { auth: true, toast: true, catch: false, ...(config.custom || {}) },
  });
}

//公告列表
export function getNoticeList(params = {}, config = {}) {
  return get("/baseplatform/home/content/list", params, {
    ...config,
    custom: {
      auth: false,
      toast: true,
      catch: false,
      ...(config.custom || {}),
    },
  });
}

//公告详情
export function getNoticeDetail(params = {}, config = {}) {
  return get(`/baseplatform/home/content/detail`, params, {
    ...config,
    custom: {
      auth: false,
      toast: true,
      catch: false,
      ...(config.custom || {}),
    },
  });
}

export function login() {
  return new Promise((resolve, reject) => {
    uni.login({
      provider: "weixin",
      success(res) {
        resolve(res);
      },
      fail(err) {
        reject(err);
      },
    });
  })
    .then((res) => {
      return get(
        "/baseplatform/wxUser/wxLogin",
        { code: res.code },
        {
          custom: {
            auth: false,
            toast: true,
            catch: true,
          },
        }
      ).then((res) => {
        uni.setStorageSync("X-Token", res.data?.accessToken);
        uni.setStorageSync("X-UserInfo", res.data?.wuserExt);
        return res;
      });
    })
    .catch((err) => {
      console.log("uni.login fail======", err);
    });
}

//订场列表
export function getBookingList(params = {}, config = {}) {
  return get("/ticket/venuesFront/searchVenuesFront", params, {
    ...config,
    custom: {
      auth: false,
      toast: true,
      catch: false,
      ...(config.custom || {}),
    },
  });
}

//订场详情
export function getBookingDetail(params = {}, config = {}) {
  return http.post("/ticket/venuesFront/searchVenuesFrontDetail", params, {
    ...config,
    custom: {
      auth: false,
      toast: true,
      catch: false,
      ...(config.custom || {}),
    },
  });
}

//字典 dictType

export function getDict(params = {}, config = {}) {
  return get("/system/dict/data/list", params, {
    ...config,
    custom: {
      auth: false,
      toast: true,
      catch: false,
      ...(config.custom || {}),
    },
  });
}

//查看场馆类别
export function getBookingType(params = {}, config = {}) {
  return get("/ticket/venuesFront/searchDictData", params, {
    ...config,
    custom: {
      auth: false,
      toast: true,
      catch: false,
      ...(config.custom || {}),
    },
  });
}

//订单列表
export function getOrderList(params = {}, config = {}) {
  return get("/ticket/appletTicket/listOrder", params, {
    ...config,
    custom: { auth: true, toast: true, catch: false, ...(config.custom || {}) },
  });
}

export function getAppletTicketList(params = {}, config = {}) {
  return http.post("/ticket/appletTicket/list", params, {
    ...config,
    custom: { auth: true, toast: true, catch: false, ...(config.custom || {}) },
  });
}

//取消订单
export function getCancleOrder(params = {}, config = {}) {
  return get("/ticket/appletTicket/cancleOrder", params, {
    ...config,
    custom: { auth: true, toast: true, catch: false, ...(config.custom || {}) },
  });
}

// 取消场馆订单
export function getCancleBookingOrder(params = {}, config = {}) {
  return get("/ticket/appletTicket/cancleVenuesOrder", params, {
    ...config,
    custom: { auth: true, toast: true, catch: false, ...(config.custom || {}) },
  });
}

//联系客服（获取客服电话）
export function getServerPhone(params = {}, config = {}) {
  return http.post("/order/order/serverPhone", params, {
    ...config,
    custom: { auth: true, toast: true, catch: false, ...(config.custom || {}) },
  });
}

//支付前获取订单详细
export function getPayOrder(params = {}, config = {}) {
  return get("/order/order/payOrder", params, {
    ...config,
    custom: { auth: true, toast: true, catch: false, ...(config.custom || {}) },
  });
}

//支付前获取订单详细
export function payPayTransactions(params = {}, config = {}) {
  return get("/pay/pay/transactions", params, {
    ...config,
    custom: { auth: true, toast: true, catch: false, ...(config.custom || {}) },
  });
}

//删除订单
export function getDeleteOrder(params = {}, config = {}) {
  return get("/ticket/appletTicket/deleteOrder", params, {
    ...config,
    custom: { auth: true, toast: true, catch: false, ...(config.custom || {}) },
  });
}

//首页各种元素
export function getElements(params = {}, config = {}) {
  return get("/baseplatform/home/elements/v2", params, {
    ...config,
    custom: {
      auth: false,
      toast: true,
      catch: false,
      ...(config.custom || {}),
    },
  });
}

export function sendCodeMessage(params = {}, config = {}) {
  return get("/baseplatform/wxAuth/sendMessage", params, {
    ...config,
    custom: { auth: true, toast: true, catch: false, ...(config.custom || {}) },
  });
}

export function checkPhoneCode(params = {}, config = {}) {
  return http.post("/baseplatform/wxAuth/checkPhoneCode", params, {
    ...config,
    custom: { auth: true, toast: true, catch: false, ...(config.custom || {}) },
  });
}

export function checkIdCard(params = {}, config = {}) {
  return http.post("/baseplatform/wxAuth/checkIdCard", params, {
    ...config,
    custom: { auth: true, toast: true, catch: false, ...(config.custom || {}) },
  });
}

export function orderIdBykey(params = {}, config = {}) {
  return get(`/order/order/orderResult`, params, {
    ...config,
    custom: { auth: true, toast: true, catch: false, ...(config.custom || {}) },
  });
}

export function getActiveOrderDetail(params = {}, config = {}) {
  return get(`/ticket/appletTicket/detailOrder`, params, {
    ...config,
    custom: { auth: true, toast: true, catch: false, ...(config.custom || {}) },
  });
}
export function ticCheckStatus(params = {}, config = {}) {
  return get(`/ticket/appletTicket/ticCheckStatus`, params, {
    ...config,
    custom: { auth: true, toast: true, catch: false, ...(config.custom || {}) },
  });
}
// 获取验证码
export function getCode(params = {}, config = {}) {
  return http.post(`/mobile/api/notice/sms/login`, params, {
    ...config,
    custom: { auth: true },
  });
}
export function loginOut(params = {}, config = {}) {
  return get(`/baseplatform/wxUser/wxLogout`, params, {
    ...config,
    custom: { auth: true },
  });
}
